package com.ymttest.database.mapping.godmember;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
import static org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT_DISTINCT;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import com.ymttest.database.model.godmember.MemberPoints;
import com.ymttest.database.model.godmember.MemberPointsExample.Criteria;
import com.ymttest.database.model.godmember.MemberPointsExample.Criterion;
import com.ymttest.database.model.godmember.MemberPointsExample;
import java.util.List;
import java.util.Map;

public class MemberPointsSqlProvider {

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table memberpoints
     *
     * @mbggenerated Tue May 19 11:10:11 CST 2020
     */
    public String countByExample(MemberPointsExample example) {
        BEGIN();
        SELECT("count(*)");
        FROM("memberpoints");
        applyWhere(example, false);
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table memberpoints
     *
     * @mbggenerated Tue May 19 11:10:11 CST 2020
     */
    public String deleteByExample(MemberPointsExample example) {
        BEGIN();
        DELETE_FROM("memberpoints");
        applyWhere(example, false);
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table memberpoints
     *
     * @mbggenerated Tue May 19 11:10:11 CST 2020
     */
    public String insertSelective(MemberPoints record) {
        BEGIN();
        INSERT_INTO("memberpoints");
        
        if (record.getMemberId() != null) {
            VALUES("MemberId", "#{memberId,jdbcType=VARCHAR}");
        }
        
        if (record.getUserId() != null) {
            VALUES("UserId", "#{userId,jdbcType=INTEGER}");
        }
        
        if (record.getShopId() != null) {
            VALUES("ShopId", "#{shopId,jdbcType=INTEGER}");
        }
        
        if (record.getAvailablePoints() != null) {
            VALUES("AvailablePoints", "#{availablePoints,jdbcType=INTEGER}");
        }
        
        if (record.getTotalPoints() != null) {
            VALUES("TotalPoints", "#{totalPoints,jdbcType=INTEGER}");
        }
        
        if (record.getOrderNum() != null) {
            VALUES("OrderNum", "#{orderNum,jdbcType=INTEGER}");
        }
        
        if (record.getOrderAmt() != null) {
            VALUES("OrderAmt", "#{orderAmt,jdbcType=DECIMAL}");
        }
        
        if (record.getLastMonthOrderAmt() != null) {
            VALUES("LastMonthOrderAmt", "#{lastMonthOrderAmt,jdbcType=DECIMAL}");
        }
        
        if (record.getLastOrderAmt() != null) {
            VALUES("LastOrderAmt", "#{lastOrderAmt,jdbcType=DECIMAL}");
        }
        
        if (record.getLastOrderTime() != null) {
            VALUES("LastOrderTime", "#{lastOrderTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getLastOrderId() != null) {
            VALUES("LastOrderId", "#{lastOrderId,jdbcType=BIGINT}");
        }
        
        if (record.getCreateTime() != null) {
            VALUES("CreateTime", "#{createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getUpdateTime() != null) {
            VALUES("UpdateTime", "#{updateTime,jdbcType=TIMESTAMP}");
        }
        
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table memberpoints
     *
     * @mbggenerated Tue May 19 11:10:11 CST 2020
     */
    public String selectByExample(MemberPointsExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("MemberId");
        } else {
            SELECT("MemberId");
        }
        SELECT("UserId");
        SELECT("ShopId");
        SELECT("AvailablePoints");
        SELECT("TotalPoints");
        SELECT("OrderNum");
        SELECT("OrderAmt");
        SELECT("LastMonthOrderAmt");
        SELECT("LastOrderAmt");
        SELECT("LastOrderTime");
        SELECT("LastOrderId");
        SELECT("CreateTime");
        SELECT("UpdateTime");
        SELECT("RefundAmt");
        SELECT("RefundNum");
        FROM("memberpoints");
        applyWhere(example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table memberpoints
     *
     * @mbggenerated Tue May 19 11:10:11 CST 2020
     */
    public String updateByExampleSelective(Map<String, Object> parameter) {
        MemberPoints record = (MemberPoints) parameter.get("record");
        MemberPointsExample example = (MemberPointsExample) parameter.get("example");
        
        BEGIN();
        UPDATE("memberpoints");
        
        if (record.getMemberId() != null) {
            SET("MemberId = #{record.memberId,jdbcType=VARCHAR}");
        }
        
        if (record.getUserId() != null) {
            SET("UserId = #{record.userId,jdbcType=INTEGER}");
        }
        
        if (record.getShopId() != null) {
            SET("ShopId = #{record.shopId,jdbcType=INTEGER}");
        }
        
        if (record.getAvailablePoints() != null) {
            SET("AvailablePoints = #{record.availablePoints,jdbcType=INTEGER}");
        }
        
        if (record.getTotalPoints() != null) {
            SET("TotalPoints = #{record.totalPoints,jdbcType=INTEGER}");
        }
        
        if (record.getOrderNum() != null) {
            SET("OrderNum = #{record.orderNum,jdbcType=INTEGER}");
        }
        
        if (record.getOrderAmt() != null) {
            SET("OrderAmt = #{record.orderAmt,jdbcType=DECIMAL}");
        }
        
        if (record.getLastMonthOrderAmt() != null) {
            SET("LastMonthOrderAmt = #{record.lastMonthOrderAmt,jdbcType=DECIMAL}");
        }
        
        if (record.getLastOrderAmt() != null) {
            SET("LastOrderAmt = #{record.lastOrderAmt,jdbcType=DECIMAL}");
        }
        
        if (record.getLastOrderTime() != null) {
            SET("LastOrderTime = #{record.lastOrderTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getLastOrderId() != null) {
            SET("LastOrderId = #{record.lastOrderId,jdbcType=BIGINT}");
        }
        
        if (record.getCreateTime() != null) {
            SET("CreateTime = #{record.createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getUpdateTime() != null) {
            SET("UpdateTime = #{record.updateTime,jdbcType=TIMESTAMP}");
        }
        
        applyWhere(example, true);
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table memberpoints
     *
     * @mbggenerated Tue May 19 11:10:11 CST 2020
     */
    public String updateByExample(Map<String, Object> parameter) {
        BEGIN();
        UPDATE("memberpoints");
        
        SET("MemberId = #{record.memberId,jdbcType=VARCHAR}");
        SET("UserId = #{record.userId,jdbcType=INTEGER}");
        SET("ShopId = #{record.shopId,jdbcType=INTEGER}");
        SET("AvailablePoints = #{record.availablePoints,jdbcType=INTEGER}");
        SET("TotalPoints = #{record.totalPoints,jdbcType=INTEGER}");
        SET("OrderNum = #{record.orderNum,jdbcType=INTEGER}");
        SET("OrderAmt = #{record.orderAmt,jdbcType=DECIMAL}");
        SET("LastMonthOrderAmt = #{record.lastMonthOrderAmt,jdbcType=DECIMAL}");
        SET("LastOrderAmt = #{record.lastOrderAmt,jdbcType=DECIMAL}");
        SET("LastOrderTime = #{record.lastOrderTime,jdbcType=TIMESTAMP}");
        SET("LastOrderId = #{record.lastOrderId,jdbcType=BIGINT}");
        SET("CreateTime = #{record.createTime,jdbcType=TIMESTAMP}");
        SET("UpdateTime = #{record.updateTime,jdbcType=TIMESTAMP}");
        
        MemberPointsExample example = (MemberPointsExample) parameter.get("example");
        applyWhere(example, true);
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table memberpoints
     *
     * @mbggenerated Tue May 19 11:10:11 CST 2020
     */
    public String updateByPrimaryKeySelective(MemberPoints record) {
        BEGIN();
        UPDATE("memberpoints");
        
        if (record.getUserId() != null) {
            SET("UserId = #{userId,jdbcType=INTEGER}");
        }
        
        if (record.getShopId() != null) {
            SET("ShopId = #{shopId,jdbcType=INTEGER}");
        }
        
        if (record.getAvailablePoints() != null) {
            SET("AvailablePoints = #{availablePoints,jdbcType=INTEGER}");
        }
        
        if (record.getTotalPoints() != null) {
            SET("TotalPoints = #{totalPoints,jdbcType=INTEGER}");
        }
        
        if (record.getOrderNum() != null) {
            SET("OrderNum = #{orderNum,jdbcType=INTEGER}");
        }
        
        if (record.getOrderAmt() != null) {
            SET("OrderAmt = #{orderAmt,jdbcType=DECIMAL}");
        }
        
        if (record.getLastMonthOrderAmt() != null) {
            SET("LastMonthOrderAmt = #{lastMonthOrderAmt,jdbcType=DECIMAL}");
        }
        
        if (record.getLastOrderAmt() != null) {
            SET("LastOrderAmt = #{lastOrderAmt,jdbcType=DECIMAL}");
        }
        
        if (record.getLastOrderTime() != null) {
            SET("LastOrderTime = #{lastOrderTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getLastOrderId() != null) {
            SET("LastOrderId = #{lastOrderId,jdbcType=BIGINT}");
        }
        
        if (record.getCreateTime() != null) {
            SET("CreateTime = #{createTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getUpdateTime() != null) {
            SET("UpdateTime = #{updateTime,jdbcType=TIMESTAMP}");
        }
        
        WHERE("MemberId = #{memberId,jdbcType=VARCHAR}");
        
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table memberpoints
     *
     * @mbggenerated Tue May 19 11:10:11 CST 2020
     */
    protected void applyWhere(MemberPointsExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            WHERE(sb.toString());
        }
    }
}